/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.latestbit.smartobjects.network.tests;

import static org.junit.Assert.*;

import java.net.URI;

import org.junit.Test;
import org.latestbit.smartobjects.SmartBus;
import org.latestbit.smartobjects.exceptions.InvalidCredentialsException;
import org.latestbit.smartobjects.network.IUserCredentials;
import org.latestbit.smartobjects.network.IHandshakeManager;
import org.latestbit.smartobjects.network.IServerLoginModule;
import org.latestbit.smartobjects.network.StdLoginConnectionAuthenticator;
import org.latestbit.smartobjects.network.StdMasterKeyConnectionAuthenticator;
import org.latestbit.smartobjects.network.tcp.tests.TCPTestClient;
import org.latestbit.smartobjects.network.tcp.tests.TCPTestServer;


public class StdHandshakeManagerTest {

	@Test
	public void testHandshakeManager() throws Exception {
		SmartBus bus = new SmartBus();
		
		URI serverURI = new URI("tcp://localhost:3333");
		TCPTestServer server = new TCPTestServer(serverURI, bus.getManagedNetwork().getNetworkFactory());
		TCPTestClient client = new TCPTestClient(serverURI, bus.getManagedNetwork().getNetworkFactory());
		
		server.getAcceptor().start();
		
		client.getListener().getConditionLock().lock();
		client.getConnector().connectAsync();
		client.getListener().waitCondition();
		

		client.getListener().getConditionLock().unlock();
		
		assertTrue ( client.getListener().isConnected() );
		
		server.getAcceptor().destroy();
		client.getConnector().destroy();
		bus.destroy();
	}
	
	@Test
	public void testHandshakeManagerWithMasterKeyAuthenticator() throws Exception {
		SmartBus bus1 = new SmartBus();
		bus1.getHandshakeManagerFactory().setConnectionAuthenticator( new StdMasterKeyConnectionAuthenticator("secret") );

		SmartBus bus2 = new SmartBus();
		bus2.getHandshakeManagerFactory().setConnectionAuthenticator( new StdMasterKeyConnectionAuthenticator("secret") );
		
		URI serverURI = new URI("tcp://localhost:3333");
		TCPTestServer server = new TCPTestServer(serverURI, bus1.getManagedNetwork().getNetworkFactory());
		TCPTestClient client = new TCPTestClient(serverURI, bus2.getManagedNetwork().getNetworkFactory());
		
		server.getAcceptor().start();
		
		client.getListener().getConditionLock().lock();
		client.getConnector().connectAsync();
		client.getListener().waitCondition();

		client.getListener().getConditionLock().unlock();
		
		assertTrue ( client.getListener().isConnected() );
		
		server.getAcceptor().destroy();
		client.getConnector().destroy();
		bus1.destroy();
		
		bus2.destroy();
	}
	
	@Test
	public void testHandshakeManagerWithLoginAuthenticator() throws Exception {
		SmartBus bus1 = new SmartBus();
		bus1.getHandshakeManagerFactory().setConnectionAuthenticator( new StdLoginConnectionAuthenticator("john","secret") );

		SmartBus bus2 = new SmartBus();
		bus2.getHandshakeManagerFactory().setConnectionAuthenticator( new StdLoginConnectionAuthenticator(
				new IServerLoginModule() {

					@Override
					public IUserCredentials authenticate( IHandshakeManager manager, String login, String password) throws InvalidCredentialsException {
						if(! (login.equals("john") && password.equals("secret")) ) {
							throw new InvalidCredentialsException();
						}
						return null;
					}
					
				}
		) );
		
		URI serverURI = new URI("tcp://localhost:3333");
		TCPTestServer server = new TCPTestServer(serverURI, bus2.getManagedNetwork().getNetworkFactory());
		TCPTestClient client = new TCPTestClient(serverURI, bus1.getManagedNetwork().getNetworkFactory());
		
		server.getAcceptor().start();
		
		client.getListener().getConditionLock().lock();
		client.getConnector().connectAsync();
		client.getListener().waitCondition();

		client.getListener().getConditionLock().unlock();
		
		assertTrue ( client.getListener().isConnected() );
		
		server.getAcceptor().destroy();
		client.getConnector().destroy();
		bus1.destroy();
		
		bus2.destroy();
	}	
	
}
